package com.health.controller;

import com.health.common.Result;
import com.health.entity.pojo.vo.TPermisionVO;
import com.health.service.ITPermisionService;
import com.health.service.ITRolePermisionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sanchuan
 * @since 2023-04-19
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/tPermision")
public class TPermisionController extends BaseController{

    @Autowired
    private ITPermisionService permisionService;

    @Autowired
    private ITRolePermisionService rolePermisionService;

    @ApiOperation("获得权限列表")
    @GetMapping("/listPermision")
    public Result<List<TPermisionVO>> listPermision(){
       return this.dataResult(permisionService.listPermision());
    }

    @ApiOperation("根据角色ID获得拥有权限")
    @GetMapping("/listPermissionsByID/{id}")
    public Result<List<Integer>> listPermissionsByID(@PathVariable("id") Integer id){
        List<Integer> permisionVOList = permisionService.listPermissionsByID(id);
        return this.dataResult(permisionVOList);
    }

    @ApiOperation("修改Permissions状态")
    @PostMapping("/changePermissionsStatus/{checked}/{id}")
    public Result<Boolean> changePermissionsStatus(@RequestBody List<Integer> checkPermissions,
                                                   @PathVariable("checked") Boolean checked,
                                                   @PathVariable("id") Integer id) {
        System.out.println(checkPermissions);
        if (checked){
            Boolean aBoolean = rolePermisionService.addPermissionsStatus(checkPermissions, id);
            return this.dataResult(aBoolean);
        }else {
            Boolean aBoolean = rolePermisionService.deletePermissionsStatus(checkPermissions, id);
            return this.dataResult(aBoolean);
        }

    }

    @ApiOperation("修改单个Permissions状态")
    @PostMapping("/changeSinglePermissionsStatus/{delId}/{isChecked}/{id}")
    public Result<Boolean> changeSinglePermissionsStatus(@PathVariable("delId") Integer delId,
                                                         @PathVariable("isChecked") Boolean isChecked,
                                                         @PathVariable("id") Integer id){
        if (isChecked){
           Boolean aBoolean =  rolePermisionService.addSinglePermissionsStatus(delId,id);
           return this.dataResult(aBoolean);
        }else {
            Boolean aBoolean =  rolePermisionService.deleteSinglePermissionsStatus(delId,id);
            return this.dataResult(aBoolean);
        }
    }
}
